package org.yu.register.capability;


import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.common.util.NonNullSupplier;
import org.yu.register.CapabilityRegister;

import javax.annotation.Nonnull;
import java.util.function.Supplier;

public class LingQiStorageProvider extends CapabilityProvider<LingQiStorageProvider> implements INBTSerializable<CompoundTag> {

    private final LazyOptional<LingQiStorage> capability;

    public LingQiStorageProvider(NonNullSupplier<LingQiStorage> supplier) {
        super(LingQiStorageProvider.class);
        capability = LazyOptional.of(supplier);
    }


    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, Direction side) {
        return CapabilityRegister.LING_QI_STORAGE.orEmpty(cap, capability.cast());
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag tag=new CompoundTag();
        if (capability.resolve().isPresent())  tag.put("LingQiStorage",capability.resolve().get().serializeNBT());
        return tag;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
            capability.ifPresent(cap -> cap.deserializeNBT(nbt.getCompound("LingQiStorage")));
    }
}
